package com.em.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.em.entity.User;
import com.em.dao.UserMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    public List<User> selectByUsername(String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNoneBlank(username)) {
            queryWrapper.like("username", username);
        }
        return userMapper.selectList(queryWrapper);
    }

    public int createUser(User user) {
        long currentTime = System.currentTimeMillis();
        user.setCreateTime(currentTime);
        user.setUpdateTime(currentTime);
        return userMapper.insert(user);
    }

    public int updateUser(User user) {
        user.setUpdateTime(System.currentTimeMillis());
        return userMapper.updateById(user);
    }

    public int deleteUser(Integer id) {
        return userMapper.deleteById(id);
    }

}
